*************************************************************************************
*************************************************************************************
** Replication code for:
** Troublesome Resentment Preregistered Questionnaire 7
** US Public Perceptions of an Intelligence Quotient Test Score Gap Between Black Americans and White Americans 
** Preregistration: https://osf.io/cx3eb/?view_only=22147e7cc9264da19c2ecdf7dcd32a60
** Stata 15
*************************************************************************************
*************************************************************************************

set more off
svyset [pw=weight]

*************************************************************************************
** Code demographics
*************************************************************************************

tab gender
gen female=gender-1
tab gender female, mi

tab race
tab race, nol
gen NHwhite=0
replace NHwhite=1 if race==1
gen NHblack=0
replace NHblack=1 if race==2
gen Hispanic=0
replace Hispanic=1 if race==3
gen RaceOther = 0
replace RaceOther = 1 if race>=4 & race<=8
tab race NHwhite, mi
tab race NHblack, mi
tab race Hispanic, mi
tab race RaceOther, mi

sum birthyr
gen age=2017-birthyr
sum age, de
gen age01=(age-r(min))/(r(max)-r(min))
sum age age01, de
pwcorr birthyr age01, sig obs

tab educ
sum educ
gen educ01=(educ-r(min))/(r(max)-r(min))
tab educ educ01, mi

tab ideo5
tab ideo5, nol
gen NHwhiteLIB=0
replace NHwhiteLIB=1 if race==1 & (ideo5==1 | ideo5==2)
gen NHwhiteCON=0
replace NHwhiteCON=1 if race==1 & (ideo5==4 | ideo5==5)
gen NHwhiteMODDK=0
replace NHwhiteMODDK=1 if race==1 & (ideo5==3 | ideo5==6)
tab race NHwhiteLIB, mi
tab race NHwhiteCON, mi
tab race NHwhiteMODDK, mi
tab ideo5 NHwhiteLIB, mi
tab ideo5 NHwhiteCON, mi
tab ideo5 NHwhiteMODDK, mi

** [Appendix 1C]
tab1 gender race educ ideo5
tab1 ideo5 if NHwhite==1
sum age, de

** [Appendix 2] For reference to the GSS and ANES
svy: tab ideo5 if NHwhite==1
di 0.0509 + 0.1468 // liberal
di 0.0730 + 0.2932 // conservative
di 0.3589          // moderate
di 0.0771          // not sure

*************************************************************************************
** Code responses for the direct item Q2b
*************************************************************************************

tab Q2_treat
tab Q2_treat, nol mi // N=508 for IQ item
tab Q2b
tab Q2b, nol mi

gen Q2iqBW=0 if Q2_treat==2
replace Q2iqBW=1 if Q2_treat==2 & Q2b==1
tab Q2_treat Q2iqBW, mi

gen Q2iqWB=0 if Q2_treat==2
replace Q2iqWB=1 if Q2_treat==2 & Q2b==2
tab Q2_treat Q2iqWB, mi

gen Q2iqEQ=0 if Q2_treat==2
replace Q2iqEQ=1 if Q2_treat==2 & Q2b==3
tab Q2_treat Q2iqEQ, mi

gen Q2iqMI=0 if Q2_treat==2
replace Q2iqMI=1 if Q2_treat==2 & (Q2b!=1 & Q2b!=2 & Q2b!=3)
tab Q2_treat Q2iqMI, mi

tab1 Q2iqBW Q2iqWB Q2iqEQ Q2iqMI

di 12 + 186 + 300 + 10 // check N=508
tab Q2_treat

*************************************************************************************
** Analyze responses for the direct item Q2b [Table 2]
*************************************************************************************

svyset [pw=weight]

svy: prop Q2iqBW // reported in the main text
svy: prop Q2iqEQ // reported in the main text
svy: prop Q2iqMI // reported in the main text
svy: prop Q2iqWB // reported in the main text

svy, subpop(NHwhite): prop Q2iqBW // reported in the main text
svy, subpop(NHwhite): prop Q2iqEQ // reported in the main text
svy, subpop(NHwhite): prop Q2iqMI // reported in the main text
svy, subpop(NHwhite): prop Q2iqWB // reported in the main text

svy:                     prop Q2iqWB, level(84) // Table 1
svy, subpop(NHwhite):    prop Q2iqWB, level(84) // Table 1
svy, subpop(NHblack):    prop Q2iqWB, level(84) // Table 1
svy, subpop(NHwhiteLIB): prop Q2iqWB, level(84) // Table 1
svy, subpop(NHwhiteCON): prop Q2iqWB, level(84) // Table 1

svy:                     prop Q2iqWB, level(95) // Table 1
svy, subpop(NHwhite):    prop Q2iqWB, level(95) // Table 1
svy, subpop(NHblack):    prop Q2iqWB, level(95) // Table 1
svy, subpop(NHwhiteLIB): prop Q2iqWB, level(95) // Table 1
svy, subpop(NHwhiteCON): prop Q2iqWB, level(95) // Table 1

svy: tab Q2iqWB if NHwhiteCON==1, ci level(84)
svy: tab Q2iqWB if NHwhiteLIB==1, ci level(84)
svy: logit Q2iqWB NHwhiteCON if NHwhiteLIB==1 | NHwhiteCON==1

*************************************************************************************
** Code responses for the hypothetical test item Q2a
*************************************************************************************

tab Q2_treat // N=998 for hypothetical IQ test item
tab Q2_treat, nol mi 
tab Q2a
tab Q2a, nol mi

gen Q2iq = .
replace Q2iq = Q2a if Q2_treat==1
tab Q2iq

tab Q2iq if NHwhite==1 & Q2iq<=100

** get skips
tab Q2iq                  // 26
tab Q2iq if NHwhite==1    // 15
tab Q2iq if NHblack==1    // 2
tab Q2iq if NHwhiteLIB==1 // 3
tab Q2iq if NHwhiteCON==1 // 6

* get 50s
tab Q2iq                  // 50%
tab Q2iq if NHwhite==1    // 55%
tab Q2iq if NHblack==1    // 26%
tab Q2iq if NHwhiteLIB==1 // 62%
tab Q2iq if NHwhiteCON==1 // 54%


*************************************************************************************
** IQ 50s [Appendix 8]
*************************************************************************************

tab Q2iq
gen iq50 = Q2iq
recode iq50 (0/49 51/100 998=0) (50=1)
tab Q2iq
tab Q2iq if iq50==1, mi
tab Q2iq if iq50==0, mi

svy: reg   iq50 female i.race age01 ib2.educ
svy: logit iq50 female i.race age01 ib2.educ

*************************************************************************************
** Analyze responses for the hypothetical test item Q2a
*************************************************************************************

svyset [pw=weight]

recode Q2iq (998=.)
tab Q2iq

svy:                     mean Q2iq, level(84)
svy, subpop(NHwhite):    mean Q2iq, level(84)
svy, subpop(NHblack):    mean Q2iq, level(84)
svy, subpop(NHwhiteLIB): mean Q2iq, level(84)
svy, subpop(NHwhiteCON): mean Q2iq, level(84)

svy:                     mean Q2iq
svy, subpop(NHwhite):    mean Q2iq 
svy, subpop(NHblack):    mean Q2iq 
svy, subpop(NHwhiteLIB): mean Q2iq 
svy, subpop(NHwhiteCON): mean Q2iq 

*************************************************************************************
** Demographic predictors for the "White Americans" response [Table 2]
*************************************************************************************

tab race
tab race, nol
gen raceCOMBO = race
recode raceCOMBO (6=5) (5 7 8 = 6)
label define racelabel 1 "White" 2 "Black" 3 "Hispanic" 4 "Asian" 5 "Mixed" 6 "Other"
label values raceCOMBO racelabel
tab race raceCOMBO, mi

svy: logit Q2iqWB educ01 age01 female i.raceCOMBO 
svy: logit Q2iqWB educ01 age01 female i.raceCOMBO ib2.pid3
svy: logit Q2iqWB educ01 age01 female i.raceCOMBO ib7.pid7
svy: logit Q2iqWB educ01 age01 female i.raceCOMBO ib5.ideo5
